<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if(!$_RQ['isdemo']){
	$this->model('model')->check_custom();
	$workerid=model('jy_scrm/worker')->get_workerid($_SESSION['openid']);
	if(!$workerid){
		exi('获取员工身份异常！'.$_SESSION['openid'],'error');
	}
}
if($_RQ['op']=='main'){
	$id=intval($_RQ['id']);
	$log=pdo_get('jy_scrmtask_log',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($workerid!=$log['workerid'] && !$_RQ['isdemo']){
		exi('该任务不是你的！','error');
	}
	pdo_update('jy_scrmtask_log',array('status'=>1,'get_time'=>TIMESTAMP),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item=pdo_get('jy_scrmtask_list',array('id'=>$log['tid']));
	$item['workers']=$item['workers']?json_decode($item['workers'],true):array();
	$item['images']=$item['images']?json_decode($item['images'],true):array();
	$item['tuwen']=$item['tuwen']?json_decode($item['tuwen'],true):array();
	$return['item']=$item;
	$return['log']=$log;
	$return['typetitle']=$return['gtypetitle']=$this->task_type[$item['type']];
	$title=$item['tuwen']['title'];
	$desc=$item['tuwen']['desc'];
	$link=custom_url($item['tuwen']['h5url'],true);
	$imgUrl=$item['tuwen']['image'];
	if($item['type']==0){
		$return['tips']='请将下面内容素材发送到自己的企业微信群！';
	}elseif($item['type']==1){
		$return['tips']='请将下面内容素材发送给下面的客户，即时跟进客户！';
		$return['gtypetitle']=$this->gtype[$item['gtype']];
	}elseif($item['type']==2){
		$return['tips']='请将下面内容素材发送企业朋友圈！';
	}elseif($item['type']==3){
		$return['tips']='请按下面内容素材，去添加好友，完成任务！';
	}
	$return['sharedata']=array(
			'title'=>$title,
			'desc'=>$desc,
			'link'=>$link,
			'imgUrl'=>$imgUrl
	);
	set_html('title','任务详情');
	exi($return);
}
if($_RQ['op']=='userlist'){
	$tid=intval($_RQ['tid']);
	$item=pdo_get('jy_scrmtask_list',array('id'=>$tid));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$workerid);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$return['params']['tid']=$tid;
	//手动群发任务
	if($item['gtype']==0){
		$params['tags like']='%'.trim($item['tags']).'%';
	}
	$time=$item['tixin_days']*24*3600;
	$day=date('m-d',time()-$time);
	//生日跟进
	if($item['gtype']==1){
		$params['birth like']='%'.$day.'%';
	}
	//结婚纪念日跟进
	if($item['gtype']==2){
		$params['jiehunday like']='%'.$day.'%';
	}
	//3-3-3回访
	if($item['gtype']==3){
		if($item['gtype3_type']==0){
			$lastbuys=$day3=strtotime(date('Y-m-d',strtotime("-3 day")));
		}
		if($item['gtype3_type']==1){
			$lastbuys=$week3=strtotime(date('Y-m-d',strtotime("-21 day")));
		}
		if($item['gtype3_type']==2){
			$lastbuys=$month3=strtotime(date('Y-m-d',strtotime("-3 month")));
		}
		$params['lastbuy']=$lastbuys;
	}
	//标签自动跟进
	if($item['gtype']==4){
		$params['tags like']='%'.trim($item['tags']).'%';
	}
	//新客户定期跟进
	if($item['gtype']==5){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$params['createtime >']=$daytime;
	}
    $orderby='createtime desc';
	$total='';
	$fields=array('id','name','avatar','mobile','gender','type','tags','workerid','external_userid','lastbuy','buydays','birthtime','jiehun');
	$return['list']=pdo_getslice('jy_scrm_member',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
			$v['createtime']=$v['createtime']?date('Y-m-d H:m',$v['createtime']):'无';
			$v['gender']=$this->sex[$v['gender']];
			$v['typetitle']=$this->log_type[$v['type']];
			if($item['gtype']==3){
				$v['buydays']=$v['buydays']?json_decode($v['buydays'],true):array();
				$v['lastbuydata']=$v['buydays'][$v['lastbuy']];
				if($item['gtype3_type']==2){
					$numtime='3月';
				}
				if($item['gtype3_type']==1){
					$numtime='3周';
				}
				if($item['gtype3_type']==0){
					$numtime='3天';
				}
				$goods=$v['lastbuydata']['goods']?:'无';
				$v['desc']='第'.$numtime.'回访，'.$goods;
				$v['desctime']=date('Y-m-d',$v['lastbuy']);
			}elseif($item['gtype']==1){
				$v['desc']='生日';
				$v['desctime']=date('Y-m-d',$v['birthtime']);
			}elseif($item['gtype']==2){
				$v['desc']='结婚纪念日';
				$v['desctime']=date('Y-m-d',$v['jiehun']);
			}
	}
	exi($return);
}
if($_RQ['op']=='getscheme'){
	$operator_userid=$_SESSION['openid'];
	$userid=$_RQ['userid'];
	$getscheme=cfc('wxwork_userapi')->getscheme($operator_userid,$userid);
	exi($getscheme);
}
if($_RQ['op']=='getaddnum'){
	$id=intval($_RQ['id']);
	$log=pdo_get('jy_scrmtask_log',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$addnum=$this->model('model')->siyu_uplog($log);
	$res=array('addnum'=>$addnum);
	exi($res);
}
if($_RQ['op']=='uplog'){
	$id=intval($_RQ['id']);
	pdo_update('jy_scrmtask_log',array('status'=>2),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('更新成功！');
}
?>